<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Front extends CI_Controller {
     function __construct(){
        parent::__construct();
        
        $this->load->library('generales');
        $this->generales->check_config();
        $this->load->model('front_m');
      }
      
      public function index(){
          $data['title'] = $this->session->userdata('title_site');
          $this->load->model('config_m');
          $enable_advice = $this->config_m->enable_advice();
          //Si mostrar avisos esta habilitado, se muestran.
          if($enable_advice == TRUE){
              $this->load->helper('date');
              $total_rows = $this->front_m->count_inmue();
              $this->load->library('pagination');
              $config['base_url'] = base_url().'avisos/listado';
              $config['total_rows'] = $total_rows;
              $config['per_page'] = 3;
              $config['uri_segment'] = 3;
              $desde = $this->uri->segment(3);
              $config['num_links'] = 2;
              $config['first_link'] = 'Primero';
              $config['last_link'] = 'Último';
              $this->pagination->initialize($config);
              $data_resume['paginador'] = $this->pagination->create_links(); 
              $data_resume['inmu_resume'] = $this->front_m->get_resume_inmue($config['per_page'],$desde);
              $data['content'] = $this->load->view('front/v_advice_preview',$data_resume,TRUE);
              $data['content_lat'] = $this->load->view('front/v_search','',TRUE); 
          }else{
              //sin no, muestra una viosta estática.
              $data['content'] = $this->load->view('front/v_no_advice','',TRUE);
          }
          $this->load->view('template/template',$data);
      }
          
      public function login(){
          //destruyo la sesion de login si existe
          $login = $this->session->userdata('is_logued_in');
          if (!empty($login)){
              $this->session->unset_userdata('is_logued_in');
          }
          $data['css'] = array('style/login') ;
          $data['title'] = 'Login a Administracion.';
          $data['content'] = $this->load->view('admin/v_login','',TRUE);
          $this->load->view('template/template-login',$data);
      }
      
      /*
       * Muestra el contenido completo de cada aviso.
       */
      public function view_adv($id_inmu){
          $this->load->model('config_m');
          $enable_advice = $this->config_m->enable_advice();
          //Si mostrar avisos esta habilitado, se muestran.
          if($enable_advice==TRUE){
          
          $this->load->library('googlemaps');
          $this->load->library('generales');
          
          $this->load->model('inmueble_m');
          $this->load->model('generales_m');
          
          $data['js'] = array('vendor/jquery.elevateZoom-3.0.3.min','vendor/microfiche','main_elevateZoom') ;
          $data['title'] = 'Aviso | Resume';
          
          $data_inmu['inmu_data'] = $this->inmueble_m->get_inmue($id_inmu);
              if(!empty($data_inmu['inmu_data'])){
                  $data['mapa'] = 'si';
                  $pais = $data_inmu['inmu_data']->nomb_pais;
                  $prov = $data_inmu['inmu_data']->nomb_prov;
                  $loca = $data_inmu['inmu_data']->nomb_loca;
                  $dire = $data_inmu['inmu_data']->dire_inmu;
                  $pais1 = $this->generales->sacarAcentos($pais);
                  $prov1 = $this->generales->sacarAcentos($prov);
                  $loca1 = $this->generales->sacarAcentos($loca);
                  $dire1 = $this->generales->sacarAcentos($dire);
                  //Si esta habilitado mostrar direccion en los avisos
                  $enable_adress = $this->config_m->enable_adress();
                  if($enable_adress==TRUE){
                      $coordenadas = $dire1.', '.$loca1.', '.$prov1.', '.$pais1;
                      $config['zoom'] = '16';
                      $data_inmu['ubicacion'] = $dire.'<br>'.$loca.'<br>'.$prov.'<br>'.$pais;
                      }else{
                      $coordenadas = $loca1.', '.$prov1.', '.$pais1;
                      $config['zoom'] = '14';
                      $data_inmu['ubicacion'] = $loca.'<br>'.$prov.'<br>'.$pais;;
                      }
                  
                  //Configuracion de plugin googlemaps
                  $config['center'] = $coordenadas;
                  $config['map_height'] = '250px';
                  $config['scrollwheel'] = FALSE;
                  $config['mapTypeControlStyle'] = 'DROPDOWN_MENU';
                  $config['geocodeCaching'] = TRUE;
                  $config['minifyJS'] = TRUE;
                  $this->googlemaps->initialize($config);
                  $marker = array();
                  $marker['position'] = $coordenadas;
                  $this->googlemaps->add_marker($marker);
                  $data_inmu['map'] = $this->googlemaps->create_map();
                  //Datos del inmueble
                  
                  //Si esta habilitado mostrar precio en los avisos
                  $enable_price = $this->config_m->enable_price();
                  if($enable_price==TRUE){
                      $precio = $data_inmu['inmu_data']->valor_inmu.' '.$data_inmu['inmu_data']->desc_mone;
                      $data_inmu['precio'] = $precio;
                  }
                  $data_inmu['inmu_data'] = $this->inmueble_m->get_inmue($id_inmu);
                  $data_inmu['inmu_amb'] = $this->inmueble_m->get_inmuamb($id_inmu);
                  $data_inmu['inmu_adic'] = $this->inmueble_m->get_inmuadic($id_inmu);
                  $data_inmu['inmu_serv'] = $this->inmueble_m->get_inmuserv($id_inmu);
                  $data_inmu['inmu_img_small'] = $this->front_m->get_name_thumb($id_inmu);
                  $data_inmu['inmu_img'] = $this->inmueble_m->get_inmuimg($id_inmu);
                  $data_inmu['inmu_prop'] = $this->inmueble_m->get_inmuprop($id_inmu);
              }
          $data['content'] = $this->load->view('front/v_advicedetail',$data_inmu,TRUE);
          $data['content_lat'] = $this->load->view('front/v_consulta','',TRUE); 
          $this->load->view('template/template',$data);
         }else{
             redirect('front/index');
         }
      }          
}//Fin de la clase Front